﻿/*-------------------------------------
//
//Copyright (C) 2007 开发二组 版权所有。
//
//文件名：CartInfo.cs
//
//
//文件功能描述：顾客预览订单页面
//
//
//创建标识：2007.11.21 翟丛兴
//创建描述：顾客预览订单页面的显示
//
//------------------------------------*/
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

/// <summary>
/// 顾客预览订单的页面
/// </summary>
public partial class Common_Controls_PreviewOrder : System.Web.UI.UserControl
{
    //保存购物车中的商品列表
    DataSet dsCartList; 

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            //判断顾客是否已输入送货信息，如果没输入，转到输入送货信息页面
            if (Session["OrderInfoModel"] == null)
            {
                Response.Write("<script language='javascript'>alert('请填写收获人信息!');</script>");
                Response.Redirect("Balance.aspx");
            }

            dsCartList = new DataSet();
            //判断是注册顾客还是匿名顾客
            if ((Session["CustomerID"] != null) && (Session["CustomerId"].ToString() !=string .Empty ) && (Session["CustomerID"].ToString() != "CUS-00000000"))
            {
                //如果是注册顾客，从数据库中取得商品列表
                localhost.CartService cartServiceObj = new localhost.CartService();
                dsCartList = cartServiceObj.GetProductList(Session["CustomerID"].ToString());
            }
            else
            {
                //如果是匿名顾客，从Session中取得商品列表
                dsCartList = (DataSet)Session["CartList"];
            }
            BindOrderInfoToPage();
        }
    }

    /// <summary>
    /// 显示订单信息到页面上
    /// </summary>
    private void BindOrderInfoToPage()
    {
        //显示订单中商品信息
        gvResult.DataSource = dsCartList;
        gvResult.DataBind();

        //购物车中商品总价格
        decimal totalPrice = 0;

        //计算商品的总价，总件数和总种数
        DataTable daProductList = dsCartList.Tables[0];
        for (int i = 0; i < daProductList.Rows.Count; i++)
        {
            totalPrice += Convert.ToDecimal(daProductList.Rows[i]["SalesPrice"]) * Convert.ToInt32(daProductList.Rows[i]["Quantity"]);
        }

        //显示订单价格信息
        lblOrderPrice.Text = totalPrice.ToString();
        localhost.OrderInfo orderInfoModelObj = new localhost.OrderInfo();
        orderInfoModelObj = (localhost.OrderInfo)Session["OrderInfoModel"];
        if (orderInfoModelObj.BillMethod == "普通平邮")
        {
            lblShipPrice.Text = "5.00";
            lblTotalPrice.Text = Convert.ToString((totalPrice + Convert.ToDecimal(5)));
            orderInfoModelObj.TotalPrice = Convert.ToDecimal(lblTotalPrice.Text.Trim());
        }
        else
        {
            lblShipPrice.Text = "15.00";
            lblTotalPrice.Text = Convert.ToString((totalPrice + Convert.ToDecimal(15)));
            orderInfoModelObj.TotalPrice = Convert.ToDecimal(lblTotalPrice.Text.Trim());
        }

        //将计算了总价的订单实体保存到session已供生成订单用
        Session["OrderInfoModel"] = orderInfoModelObj;

        //显示订单送货信息
        lblSenderName.Text = orderInfoModelObj.ShipToFirstName + " "+ orderInfoModelObj.ShipToLastName;
        if (lblSenderName.Text.Length > 25)
        {
            lblSenderName.Text = orderInfoModelObj.ShipToFirstName + "<br/>" + orderInfoModelObj.ShipToLastName;
        }
        lblBillWay.Text = orderInfoModelObj.BillMethod;
        lblShipWay.Text = orderInfoModelObj.ShipMethod;
        lblShipZip.Text = orderInfoModelObj.ShipZip;
        lblCreationTime.Text = DateTime.Now.ToShortDateString();
        lblPhone.Text = orderInfoModelObj.Phone;
        string strAddress = orderInfoModelObj.ShipAddress;
        if (strAddress.Length > 25)
        {
            strAddress = strAddress.Substring(0, 24) + "<br/>" + strAddress.Substring(24, strAddress.Length - 24);
        } 
        lblAddress.Text = orderInfoModelObj.ShipProvince +" "+ orderInfoModelObj.ShipCity + strAddress;
    }

    /// <summary>
    /// 确认生成订单，跳转到生成订单页面
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Button2_Click(object sender, EventArgs e)
    {
        Response.Redirect("FinishOrder.aspx");
    }

    /// <summary>
    /// 跳转到填写送货信息页面
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Redirect("Balance.aspx");
    }
}
